package com.wantong.jianpu.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;

import com.wantong.jianpu.base.BaseResultBean;
import com.wantong.jianpu.beans.PersonBean;
import com.wantong.jianpu.beans.RankBean;
import com.wantong.jianpu.event.FaultDetailEvent;
import com.wantong.jianpu.event.FaultListEvent;
import com.wantong.jianpu.models.FaultModel;
import com.wantong.jianpu.net.BaseObserver;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * @author PACY
 * @date 2022/5/6
 * description:
 */
public class AssignViewModel extends BaseViewModel {


    public String repairOrderId;//故障id

    //维修等级
    public List<RankBean> rankBeans;
    public SingleLiveEvent<String> rank=new SingleLiveEvent<>();
    public SingleLiveEvent<String[]> rankDialogEvent=new SingleLiveEvent<>();
    public String maintainLevelId;//维修等级id

    //维修人员
    public List<PersonBean> userBeans;
    public SingleLiveEvent<String> user=new SingleLiveEvent<>();
    public SingleLiveEvent<String[]> userDialogEvent=new SingleLiveEvent<>();
    public int repairUser;//维修人员id

    //协助人员
    public SingleLiveEvent<String> assistUser=new SingleLiveEvent<>();
    public SingleLiveEvent<String[]> assistDialogEvent=new SingleLiveEvent<>();
    public int helpUsers;
    public FaultModel model;

    public AssignViewModel(@NonNull Application application) {
        super(application);
        model=new FaultModel();
    }

    @Override
    protected void init() {
        super.init();
        getRankRequest();
        getUserRequest();
    }

    /**
     * 提交
     */
    public BindingCommand commit=new BindingCommand(() -> {
        dispatch();
    });

    /**
     * 筛选维修等级
     */
    public BindingCommand selectRank=new BindingCommand(() -> {
        if (rankBeans!= null) {
            String[] brand = new String[rankBeans.size()];
            for (int i = 0; i < rankBeans.size(); i++) {
                brand[i] = rankBeans.get(i).getName();
            }
            rankDialogEvent.setValue(brand);
        } else {
            getRankRequest();
        }
    });

    /**
     * 维修等级选择
     */
    public void setRankPosition(int position) {
        if (rankBeans != null) {
            rank.setValue(rankBeans.get(position).getName());
            maintainLevelId=rankBeans.get(position).getId();
        }
    }



    /**
     * 筛选维修人员
     */
    public BindingCommand selectUser=new BindingCommand(() -> {
        if (userBeans!= null) {
            String[] brand = new String[userBeans.size()];
            for (int i = 0; i < userBeans.size(); i++) {
                brand[i] = userBeans.get(i).getName();
            }
            userDialogEvent.setValue(brand);
        } else {
            getUserRequest();
        }
    });
    /**
     * 维修人员选择
     */
    public void setUserPosition(int position) {
        if (userBeans != null) {
            user.setValue(userBeans.get(position).getName());
            repairUser=userBeans.get(position).getId();
        }
    }


    /**
     * 筛选协助人员
     */
    public BindingCommand selectAssistUser=new BindingCommand(() -> {
        if (userBeans!= null) {
            String[] brand = new String[userBeans.size()];
            for (int i = 0; i < userBeans.size(); i++) {
                brand[i] = userBeans.get(i).getName();
            }
            assistDialogEvent.setValue(brand);
        } else {
            getUserRequest();
        }
    });
    /**
     * 协助人员选择
     */
    public void setAssistUserPosition(int position) {
        if (userBeans != null) {
            assistUser.setValue(userBeans.get(position).getName());
            helpUsers=userBeans.get(position).getId();
        }
    }



    /**
     * 获取维修等级
     */
    public void getRankRequest(){
        showDialog();
        model.rankList(new BaseObserver<List<RankBean>>() {
            @Override
            public void onSuccess(BaseResultBean<List<RankBean>> t) {
                rankBeans=t.getData();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }


    /**
     * 获取维修人员
     */
    public void getUserRequest(){
        showDialog();
        model.userList(new BaseObserver<List<PersonBean>>() {
            @Override
            public void onSuccess(BaseResultBean<List<PersonBean>> t) {
                userBeans=t.getData();
            }
            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }


    /**
     * 指派维修人员
     */
    public void dispatch(){
        if(StringUtils.isEmpty(maintainLevelId)){
            ToastUtils.show("请选择维修等级");
            return;
        }
        if(repairUser==0){
            ToastUtils.show("请选择维修人员");
            return;
        }

        if(repairUser==helpUsers){
            ToastUtils.show("维修人员和协助人员不能相同");
            return;
        }

        Map<String,Object> map=new HashMap<>();
        map.put("repairOrderId",repairOrderId);
        map.put("maintainLevelId",maintainLevelId);
        map.put("repairUser",repairUser);
        if(helpUsers!=0){
            map.put("helpUsers",helpUsers);
        }
        showDialog();
        model.dispatch(map, new BaseObserver() {
            @Override
            public void onSuccess(BaseResultBean t) {
                EventBus.getDefault().post(new FaultDetailEvent());
                EventBus.getDefault().post(new FaultListEvent());
                ToastUtils.show("指派成功");
                finish();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());

    }

}
